package com.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.Util.WebUtil;
import com.erp.comm.DataAndView;

import com.erp.comm.ResultCode;
import com.erp.pojo.Goods;
import com.erp.pojo.Inport;
import com.erp.pojo.Provider;
import com.erp.pojo.User;
import com.erp.service.GoodsService;
import com.erp.service.InportService;
import com.erp.service.ProviderService;
import com.erp.vo.InportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("inport")
public class InportController {

    @Autowired
    private InportService inportService;
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ProviderService providerService;

    @RequestMapping("loadAllInport")
    public DataAndView loadAllInport(InportVo inportVo) {
        System.out.println(inportVo.getStartTime());
        IPage<Inport> page = new Page<Inport>(inportVo.getPage(), inportVo.getLimit());


        QueryWrapper<Inport> qw = new QueryWrapper<>();
        //如果条件不为空 就添加
        if (inportVo.getProviderid() != null && inportVo.getProviderid()!=0){
            qw.like("providerid",inportVo.getProviderid());
        }
        if (inportVo.getProviderid() != null && inportVo.getGoodsid() !=0){
            qw.like("goodsid",inportVo.getGoodsid());
        }

        //把string转化为date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;
        if (inportVo.getStartTime()!=null && inportVo.getStartTime() != ""){
            try {
                startTime = sdf.parse(inportVo.getStartTime());
            }catch (Exception e){
            }
            qw.gt("inputtime",new Timestamp(startTime.getTime()));
        }
        if (inportVo.getEndTime()!=null && inportVo.getEndTime() != ""){
            try {
                endTime = sdf.parse(inportVo.getEndTime());
            }catch (Exception e){
            }
            qw.lt("inputtime",new Timestamp(endTime.getTime()));

        }

        if (inportVo.getOperateperson() != null){

            qw.like("operateperson",inportVo.getOperateperson());
        }
        if (inportVo.getRemark() != null){
            qw.like("remark",inportVo.getRemark());
        }

        //查询
        inportService.page(page,qw);
        List<Inport> list = page.getRecords();
        List<Inport> list2 = new ArrayList<>();
        for (Inport inport:list){
            //获取当前商品的供应商id
            Integer provideId = inport.getProviderid();
            //通过供应商查id对应的供应商
            Provider provider = providerService.getById(provideId);
            //设置当前的商品的供货名称
            inport.setProvidername(provider.getProvidername());

            Integer goodsId = inport.getGoodsid();
            Goods good = goodsService.getById(goodsId);
            //设置商品名称
            inport.setGoodsname(good.getGoodsname());
            //设置商品规格
            inport.setSize(good.getSize());

            list2.add(inport);
        }
        return new DataAndView(list2);
    }
    @RequestMapping("addInport")
    public ResultCode addInport(Inport inport){
        inport.setId(null);


        //设置操作员信息
        HttpSession session = WebUtil.getSession();
        User user = (User) session.getAttribute("user");
        inport.setOperateperson(user.getName());
        inport.setInporttime(new Date());

        boolean bool = inportService.save(inport);
        if (bool){
            return ResultCode.SUCCESS_ADD;
        } else {
            return ResultCode.FAIL_ADD;
        }
    }
    @RequestMapping("updateInport")
    public ResultCode updateGoods(Inport inport) {
        try {

            boolean bool = inportService.updateById(inport);
            if (bool) {
                return ResultCode.SUCCESS_UPDATE;
            } else {
                return ResultCode.FAIL_UPDATE;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_ADD;
        }
    }
    @RequestMapping("deleteInport")
    public ResultCode deleteInport(Inport id){
        try {
            boolean bool = inportService.removeById(id);
            if (bool) {
                return ResultCode.SUCCESS_DELETE;
            } else {
                return ResultCode.FAIL_DELETE;
            }
        } catch (Exception e) {
            return ResultCode.FAIL_DELETE;
        }
    }
}

